﻿using System;
using System.Diagnostics;
using System.IO;

namespace Qiao.Common.Core
{
    /// <summary>
    /// 文件操作帮助类
    /// </summary>
    public class FileExtend
    {
        #region Get

        #region 获取文件的详细信息
        /// <summary>
        /// 获取文件的详细信息
        /// </summary>
        /// <param name="directory">文件的目录地址</param>
        /// <returns></returns>
        public static FileInfoModel GetFileInfo(string directory)
        {
            FileInfoModel model = null;
            try
            {
                model = new FileInfoModel();
                FileInfo fi = new FileInfo(directory);
                model.Name = fi.Name;
                model.Length = fi.Length;
                model.LastWriteTime = fi.LastWriteTime;
                model.LastAccessTime = fi.LastAccessTime;
                model.IsReadOnly = fi.IsReadOnly;
                model.FullName = fi.FullName;
                model.Extension = fi.Extension;
                model.DirectoryName = fi.DirectoryName;
                model.CreationTime = fi.CreationTime;
                model.Attributes = fi.Attributes;
            }
            catch (Exception e)
            {

            }
            return model;
        }
        #endregion

        #region 获取文件的版本信息
        /// <summary>
        /// 获取文件的版本信息
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static FileVersionInfo GetFileVersionInfo(string directory)
        {
            FileVersionInfo fvi = null;
            try
            {
                fvi = FileVersionInfo.GetVersionInfo(directory);
            }
            catch (Exception e)
            {

            }
            return fvi;
        }
        #endregion

        #endregion

        #region Is

        #region 判断文件是否是只读文件
        /// <summary>
        /// 判断文件是否是只读文件
        /// </summary>
        /// <param name="directory">文件的目录地址</param>
        /// <returns></returns>
        public static bool FileIsReadOnly(string directory)
        {
            return FileExtend.FileInfo(directory).Attributes == FileAttributes.ReadOnly;
        }
        #endregion

        #region 判断文件是否是系统文件
        /// <summary>
        /// 判断文件是否是系统文件
        /// </summary>
        /// <param name="directory">文件的目录地址</param>
        /// <returns></returns>
        public static bool FileIsSystem(string directory)
        {
            return FileExtend.FileInfo(directory).Attributes == FileAttributes.System;
        }
        #endregion

        #region 判断文件是否是隐藏文件
        /// <summary>
        /// 判断文件是否是隐藏文件
        /// </summary>
        /// <param name="directory">文件的目录地址</param>
        /// <returns></returns>
        public static bool FileIsHidden(string directory)
        {
            return FileExtend.FileInfo(directory).Attributes == FileAttributes.Hidden;
        }
        #endregion

        #region 判断文件是否是文本文件
        /// <summary>
        /// 判断文件是否是文本文件
        /// </summary>
        /// <param name="directory">文件的目录地址</param>
        /// <returns></returns>
        public static bool FileIsArchive(string directory)
        {
            return FileExtend.FileInfo(directory).Attributes == FileAttributes.Archive;
        }
        #endregion 

        #endregion

        #region private

        #region 获取文件
        /// <summary>
        /// 获取文件
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        private static FileInfo FileInfo(string directory)
        {
            return new System.IO.FileInfo(directory);
        }
        #endregion

        #endregion
    }
}
